package com.atguigu.flink.day08;

import com.atguigu.flink.bean.WaterSensor;
import com.atguigu.flink.func.WaterSensorMapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Felix
 * @date 2024/8/19
 * 该案例演示了监控状态——值状态 的错误思路
 * 需求：检测每种传感器的水位值，如果连续的两个水位差值超过10，就输出报警
 */
public class Flink05_KeyedState_ValueState_1 {
    public static void main(String[] args) throws Exception {
        //TODO 1.环境准备
        //1.1 指定流处理环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //1.2 设置并行度
        env.setParallelism(1);
        //TODO 2.从指定的网络端口读取数据
        DataStreamSource<String> socketDS = env.socketTextStream("hadoop102", 8888);
        //TODO 3.对流中数据进行类型转换    String->WaterSensor
        SingleOutputStreamOperator<WaterSensor> wsDS = socketDS.map(new WaterSensorMapFunction());
        //TODO 4.按照传感器id进行分组
        KeyedStream<WaterSensor, String> keyedDS = wsDS.keyBy(WaterSensor::getId);
        //TODO 5.判断连续的两个水位差值是否超过10
        /*keyedDS.process(
                new KeyedProcessFunction<String, WaterSensor, String>() {
                    //成员变量的作用范围，每一个并行子任务(slot),会被同一个slot下的多个组共享
                    Integer lastVc = 0;
                    @Override
                    public void processElement(WaterSensor ws, KeyedProcessFunction<String, WaterSensor, String>.Context ctx, Collector<String> out) throws Exception {
                        //获取当前水位值
                        Integer curVc = ws.getVc();
                        if(Math.abs(curVc - lastVc) > 10){
                            out.collect("传感器"+ctx.getCurrentKey()+"当前水位"+curVc+"和上次水位"+lastVc+"之差大于10");
                        }
                        lastVc = curVc;
                    }
                }
        ).printToErr();*/

        keyedDS.process(
                new KeyedProcessFunction<String, WaterSensor, String>() {
                    //成员变量的作用范围，每一个并行子任务(slot),会被同一个slot下的多个组共享
                    Map<String,Integer> lastVcMap = new HashMap<>();
                    @Override
                    public void processElement(WaterSensor ws, KeyedProcessFunction<String, WaterSensor, String>.Context ctx, Collector<String> out) throws Exception {
                        String id = ws.getId();
                        Integer lastVc = lastVcMap.get(id) == null ? 0 : lastVcMap.get(id);
                        //获取当前水位值
                        Integer curVc = ws.getVc();
                        if(Math.abs(curVc - lastVc) > 10){
                            out.collect("传感器"+ctx.getCurrentKey()+"当前水位"+curVc+"和上次水位"+lastVc+"之差大于10");
                        }
                        lastVcMap.put(id,curVc);
                    }
                }
        ).printToErr();


        env.execute();

    }
}
